package com.chuangjiangx.member.manager.client.web.stored.controller;

import com.chuangjiangx.commons.response.CamelResponse;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.client.web.stored.request.*;
import com.chuangjiangx.member.manager.client.web.stored.response.MbrPayResponse;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.stored.ddd.application.MbrPayApplication;
import com.chuangjiangx.member.stored.ddd.application.command.*;
import com.chuangjiangx.member.stored.ddd.application.dto.MbrPayOrderResult;
import com.chuangjiangx.member.stored.ddd.application.dto.MbrUniformOrdersResult;
import com.chuangjiangx.member.stored.ddd.domain.exception.MemberPaymentException;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author cj-ch
 * @date 2018/4/4 下午2:34
 */
@RestController
@Api(value = "会员支付相关",tags = "支付")
@RequestMapping(method = RequestMethod.POST,value = "/mbr/pay",produces = "application/json;charset=UTF-8")
public class MbrPayController {


    @Autowired
    private MbrPayApplication mbrPayApplication;




    /**
     * 会员统一下单接口
     * @param request 下单请求对象
     * @return com.chuangjiangx.commons.response.CamelResponse
     */
    @ApiOperation(value = "会员统一下单接口",httpMethod = "POST")
    @RequestMapping(value = "/uniform-orders",method = RequestMethod.POST)
    @Login
    public CamelResponse<MbrUniformOrdersResult> uniformOrders(@Validated @RequestBody MbrUniformOrdersRequest request){
        ThreadLocalUser tokenVo = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);

        MbrUniformOrdersCommand command = new MbrUniformOrdersCommand();
        command.setType(request.getType());
        command.setAmount(request.getAmount());
        command.setCouponCode(request.getCouponCode());
        command.setRechargeRuleId(request.getRechargeRuleId());
        command.setRechargeMemberId(request.getRechargeMemberId());

        command.setMerchantId(tokenVo.getMerchantId());
        command.setMerchantUserId(tokenVo.getMerchantUserId());
        command.setStoreUserId(tokenVo.getStoreUserId());
        command.setStoreId(tokenVo.getStoreId());

        //下单
        MbrUniformOrdersResult result = mbrPayApplication.uniformOrders(command);
        return (CamelResponse<MbrUniformOrdersResult>)ResponseUtils.successCamel(result);
    }

    /**
     * 会员卡支付订单
     * 使用会员付款码完成对统一下单进行支付
     * @param payOrder
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @see MbrPayResponse
     */
    @ApiOperation(value = "会员卡支付订单")
    @RequestMapping(value = "/pay-order",method = RequestMethod.POST)
    @Login
    public CamelResponse<MbrPayResponse> payOrder(@Validated @RequestBody MbrPayOrderRequest payOrder){
        MbrPayResponse response = new MbrPayResponse();
        MbrPayOrderCommand command = new MbrPayOrderCommand();
        command.setAuthCode(payOrder.getAuthCode());
        command.setOrderNumber(payOrder.getOrderNumber());
        command.setPayTerminal(payOrder.getPayTerminal());

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        command.setMerchantId(user.getMerchantId());
        command.setMerchantUserId(user.getMerchantUserId());
        command.setStoreId(user.getStoreId());
        command.setStoreUserId(user.getStoreUserId());
        command.setStoreAddress(user.getStoreAddress());
        response.setStoreName(user.getStoreName());
        response.setCashierName(user.getRealName());
        command.setStoreAddress(user.getStoreAddress());
        MbrPayOrderResult mbrPayOrderResult = mbrPayApplication.payOrder(command);
        BeanUtils.copyProperties(mbrPayOrderResult,response);
        response.setStoreName(StringUtils.defaultIfBlank(user.getStoreName(),user.getMerchantName()));
        response.setCashierName(user.getRealName());
        return (CamelResponse<MbrPayResponse>)ResponseUtils.successCamel(response);
    }

    /**
     * 会员(b扫c)快捷支付
     * @param quickPay
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @see MbrPayResponse
     */
    @ApiOperation(value = "会员卡(b扫c)快捷支付")
    @RequestMapping(value = "/quick-pay",method = RequestMethod.POST)
    @Login
    public CamelResponse<MbrPayResponse> quickPay(@RequestBody @Validated MbrQuickPayRequest quickPay){
        //参数校验
        boolean verify = StringUtils.isBlank(quickPay.getAuthCode())
                && (StringUtils.isBlank(quickPay.getMemberMobileLast4()) || null == quickPay.getMemberId());
        if(verify){
            throw new MemberPaymentException("","(会员付款码不能为空)或(会员Id和会员手机号码后4位不能为空)");
        }
        MbrPayResponse response = new MbrPayResponse();
        MbrQuickPayCommand command = new MbrQuickPayCommand();
        command.setAmount(quickPay.getAmount());
        command.setAuthCode(quickPay.getAuthCode());
        command.setMemberId(quickPay.getMemberId());
        command.setMemberMobileLast4(quickPay.getMemberMobileLast4());
        command.setPayTerminal(quickPay.getPayTerminal());
        command.setCouponCode(quickPay.getCouponCode());
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        command.setMerchantId(user.getMerchantId());
        command.setMerchantUserId(user.getMerchantUserId());
        command.setStoreId(user.getStoreId());
        command.setStoreUserId(user.getStoreUserId());
        command.setStoreAddress(user.getStoreAddress());

        response.setCashierName(user.getRealName());
        response.setStoreName(user.getStoreName());

        MbrPayOrderResult mbrPayOrderResult = mbrPayApplication.quickPay(command);
        BeanUtils.copyProperties(mbrPayOrderResult,response);
        response.setStoreName(StringUtils.defaultIfBlank(user.getStoreName(),user.getMerchantName()));
        response.setCashierName(user.getRealName());
        return (CamelResponse<MbrPayResponse>)ResponseUtils.successCamel(response);
    }

    @ApiOperation(value = "会员(c扫b)快捷支付")
    @RequestMapping(value = "/ctob/quick-pay",method = RequestMethod.POST)
    @Login
    public CamelResponse<MbrPayResponse> ctobQuickPay(@RequestBody @Validated MbrCtobQuickPayRequest ctobQuickPayRequest){
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        MbrPayResponse response = new MbrPayResponse();
        MbrWebPagePayCommand command = new MbrWebPagePayCommand();
        command.setMerchantUserId(user.getMerchantUserId());
        command.setMerchantId(user.getMerchantId());
        command.setStoreId(user.getStoreId());
        command.setStoreUserId(user.getStoreUserId());
        command.setAmount(ctobQuickPayRequest.getAmount());
        command.setMemberId(ctobQuickPayRequest.getMemberId());
        command.setCouponCode(ctobQuickPayRequest.getCouponCode());
        command.setPayTerminal(ctobQuickPayRequest.getPayTerminal());

        MbrPayOrderResult mbrPayOrderResult = mbrPayApplication.webPagePay(command);
        BeanUtils.copyProperties(mbrPayOrderResult,response);
        response.setStoreName(StringUtils.defaultIfBlank(user.getStoreName(),user.getMerchantName()));
        response.setCashierName(user.getRealName());
        return (CamelResponse<MbrPayResponse>)ResponseUtils.successCamel(response);
    }

    /**
     * 订单撤销接口
     * @param id 会员订单Id
     * @return
     */
    @RequestMapping(value = "/undo/{id}",method = RequestMethod.POST)
    @Login
    @ApiOperation(value = "订单撤销接口")
    public CamelResponse undo(
            @ApiParam(value = "会员订单Id")@PathVariable Long id
    ){
        MbrOrderUndoCommand c = new MbrOrderUndoCommand();
        c.setMbrOrderId(id);
        ThreadLocalUser tokenVo = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        c.setMerchantId(tokenVo.getMerchantId());
        if(null != tokenVo.getStoreUserId()){
            c.setStoreId(tokenVo.getStoreId());
            c.setStoreUserId(tokenVo.getStoreUserId());
            //店长店员都当成了店长
            c.setUserType(1);
        }else{
            c.setUserType(0);
        }
        mbrPayApplication.undo(c);
        return (CamelResponse) ResponseUtils.successCamel();
    }

    /**
     * 会员消费记录--编辑备注
     * @param request
     * @return
     */
    @ApiOperation(value = "会员消费记录--编辑备注")
    @RequestMapping(value = "/edit-remark",method = RequestMethod.POST)
    @Login
    public CamelResponse editRemark(@Validated @RequestBody MbrOrderEditRemarkRequest request){

        MbrOrderEditRemarkCommand command = new MbrOrderEditRemarkCommand();
        command.setId(request.getId());
        command.setRemark(request.getRemark());
        mbrPayApplication.editRemark(command);
        return (CamelResponse) ResponseUtils.successCamel();
    }
}
